'use strict';

exports.__esModule = true;
exports.createMapContext = createMapContext;
exports.createLivePusherContext = createLivePusherContext;
exports.createLivePlayerContext = createLivePlayerContext;
exports.createCameraContext = createCameraContext;
exports.createVideoContext = createVideoContext;
exports.createInnerAudioContext = createInnerAudioContext;
exports.getRecorderManager = getRecorderManager;
exports.getBackgroundAudioManager = getBackgroundAudioManager;
exports.navigateTo = navigateTo;
exports.redirectTo = redirectTo;
exports.changeTo = changeTo;
exports.changeToPage = changeToPage;
exports.switchTab = switchTab;
exports.switchPage = switchPage;
exports.navigateBack = navigateBack;
exports.reLaunch = reLaunch;
exports.getCurUrl = getCurUrl;
exports.initWx = initWx;
exports.setNavBarTitle = setNavBarTitle;
exports.stopPullDownRefresh = stopPullDownRefresh;
exports.showPageLoading = showPageLoading;
exports.setStorageSync = setStorageSync;
exports.getStorageSync = getStorageSync;
exports.getStorageInfoSync = getStorageInfoSync;
exports.startAccelerometer = startAccelerometer;
exports.stopAccelerometer = stopAccelerometer;
exports.startCompass = startCompass;
exports.stopCompass = stopCompass;
exports.removeStorageSync = removeStorageSync;
exports.clearStorageSync = clearStorageSync;
exports.getSystemInfoSync = getSystemInfoSync;
exports.hidePageLoading = hidePageLoading;
exports.pageScrollTo = pageScrollTo;
exports.showModal = showModal;
exports.compareVersion = compareVersion;
var __curUrl = "";
var createPromisedFun = function createPromisedFun(name) {
    var d = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

    return function (opt) {
        return new Promise(function (resolve, reject) {
            wx[name](Object.assign({
                success: function success(res) {
                    resolve(res);
                },
                fail: function fail(e) {
                    reject(e);
                }
            }, d, opt));
        });
    };
};

var createParamFun = function createParamFun(fun) {
    setNavigationBarTitle();
};

var createFun = function createFun(name) {
    return function () {
        var opt = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

        wx[name](Object.assign({}, opt));
    };
};

var scanCode = exports.scanCode = createPromisedFun('scanCode');
var previewImage = exports.previewImage = createPromisedFun('previewImage');
var getImageInfo = exports.getImageInfo = createPromisedFun('getImageInfo');
var saveImageToPhotosAlbum = exports.saveImageToPhotosAlbum = createPromisedFun('saveImageToPhotosAlbum');
var chooseVideo = exports.chooseVideo = createPromisedFun('chooseVideo');
var saveVideoToPhotosAlbum = exports.saveVideoToPhotosAlbum = createPromisedFun('saveVideoToPhotosAlbum');
var chooseImage = exports.chooseImage = createPromisedFun('chooseImage');
var getFileInfo = exports.getFileInfo = createPromisedFun('getFileInfo');
var getSavedFileList = exports.getSavedFileList = createPromisedFun('getSavedFileList');
var getSavedFileInfo = exports.getSavedFileInfo = createPromisedFun('getSavedFileInfo');
var removeSavedFile = exports.removeSavedFile = createPromisedFun('removeSavedFile');
var openDocument = exports.openDocument = createPromisedFun('openDocument');
var getLocation = exports.getLocation = createPromisedFun('getLocation');
var chooseLocation = exports.chooseLocation = createPromisedFun('chooseLocation');
var openLocation = exports.openLocation = createPromisedFun('openLocation');
var getNetworkType = exports.getNetworkType = createPromisedFun('getNetworkType');
var setClipboardData = exports.setClipboardData = createPromisedFun('setClipboardData');
var getClipboardData = exports.getClipboardData = createPromisedFun('getClipboardData');
var setScreenBrightness = exports.setScreenBrightness = createPromisedFun('setScreenBrightness');
var getScreenBrightness = exports.getScreenBrightness = createPromisedFun('getScreenBrightness');
var keepScreenOn = exports.keepScreenOn = createPromisedFun('keepScreenOn');
var addPhoneContact = exports.addPhoneContact = createPromisedFun('addPhoneContact');
var showLoading = exports.showLoading = createPromisedFun('showLoading');
var setNavigationBarTitle = exports.setNavigationBarTitle = createPromisedFun('setNavigationBarTitle');
var setTabBarBadge = exports.setTabBarBadge = createPromisedFun('setTabBarBadge');
var removeTabBarBadge = exports.removeTabBarBadge = createPromisedFun('removeTabBarBadge');
var showTabBarRedDot = exports.showTabBarRedDot = createPromisedFun('showTabBarRedDot');
var hideTabBarRedDot = exports.hideTabBarRedDot = createPromisedFun('hideTabBarRedDot');
var setTabBarStyle = exports.setTabBarStyle = createPromisedFun('setTabBarStyle');
var setTabBarItem = exports.setTabBarItem = createPromisedFun('setTabBarItem');
var showTabBar = exports.showTabBar = createPromisedFun('showTabBar');
var hideTabBar = exports.hideTabBar = createPromisedFun('hideTabBar');
var requestPayment = exports.requestPayment = createPromisedFun('requestPayment');
var showToast = exports.showToast = createPromisedFun('showToast', {
    icon: "none"
});
var hideToast = exports.hideToast = createFun('hideToast');
var hideLoading = exports.hideLoading = createFun('hideLoading');

var showNavigationBarLoading = exports.showNavigationBarLoading = createFun('showNavigationBarLoading');
var hideNavigationBarLoading = exports.hideNavigationBarLoading = createFun('hideNavigationBarLoading');
var makePhoneCall = exports.makePhoneCall = createFun('makePhoneCall');

function createMapContext() {
    var mapContext = wx.createMapContext();
    return mapContext;
}

function createLivePusherContext() {
    var livePusherContext = wx.createLivePusherContext();
    return livePusherContext;
}

function createLivePlayerContext() {
    var livePlayerContext = wx.createLivePlayerContext();
    return livePlayerContext;
}

function createCameraContext() {
    var cameraContext = wx.createCameraContext();
    return cameraContext;
}

function createVideoContext() {
    var videoContext = wx.createVideoContext();
    return videoContext;
}

function createInnerAudioContext() {
    var innerAudioContext = wx.createInnerAudioContext();
    return innerAudioContext;
}

function getRecorderManager() {
    var recorderManager = wx.getRecorderManager();
    return recorderManager;
}

function getBackgroundAudioManager() {
    var backgroundAudioManager = wx.getBackgroundAudioManager();
    return backgroundAudioManager;
}

var updateApp = exports.updateApp = function updateApp(opt) {
    var updateManager = wx.getUpdateManager();
    updateManager.onUpdateReady(function () {
        wx.showModal(Object.assign({
            title: '更新提示',
            content: '新版本已经准备好，是否重启应用？',
            success: function success(res) {
                if (res.confirm) {
                    updateManager.applyUpdate();
                }
            }
        }, opt));
    });
};

function navigateTo(url) {
    if (url) {
        __curUrl = url;
        wx.setStorageSync("__curUrl", url);
        wx.navigateTo({
            url: url
        });
    } else {
        throw new Error("url not empty");
    }
}

function redirectTo(url) {
    wx.setStorageSync("__curUrl", url);
    wx.redirectTo({
        url: url
    });
}

function changeTo(url) {
    var opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

    var fun = navigateTo;
    if (opt && opt.type === "redirect") {
        fun = redirectTo;
    }
    return fun(url);
}

function changeToPage(pageName) {
    var p = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "index";
    var opt = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};

    return changeTo('/pages/' + pageName + '/' + p, opt);
}

function switchTab(url, opt) {
    wx.setStorageSync("__curUrl", url);
    wx.switchTab({
        url: url
    });
}

function switchPage(pageName) {
    var p = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "index";
    var opt = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};

    switchTab('/pages/' + pageName + '/' + p, opt);
}

function navigateBack(delta) {
    wx.navigateBack({
        delta: delta
    });
}

function reLaunch(url) {
    wx.reLaunch(url);
}

function getCurUrl() {
    return wx.getStorageSync("__curUrl");
}

function initWx() {
    wx.removeStorageSync("__curUrl");
}

var uploadFile = exports.uploadFile = function uploadFile(url, filePath) {
    var onSend = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : function (u) {};
    var formData = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};

    return new Promise(function (resolve, reject) {
        var uploadTask = wx.uploadFile({
            url: url,
            filePath: filePath,
            name: 'file',
            formData: formData,
            success: function success(res) {
                resolve(res, uploadTask);
            },
            fail: function fail(e) {
                reject(e, uploadTask);
            }
        });
        if (onSend) {
            onSend(uploadTask);
        }
    });
};

var downloadFile = exports.downloadFile = function downloadFile(url, filePath) {
    var onSend = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : function (u) {};

    return new Promise(function (resolve, reject) {
        var downloadTask = wx.downloadFile({
            url: url,
            success: function success(res) {
                resolve(res, downloadTask);
            },
            fail: function fail(e) {
                reject(e, downloadTask);
            }
        });
        if (onSend) {
            onSend(downloadTask);
        }
    });
};

function setNavBarTitle(title) {
    wx.setNavigationBarTitle({
        title: title
    });
}

function stopPullDownRefresh() {
    wx.stopPullDownRefresh();
}

function showPageLoading() {
    var text = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "加载中";

    showNavigationBarLoading();
    wx.showLoading({
        title: text
    });
}

function setStorageSync(k, v) {
    wx.setStorageSync(k, v);
}

function getStorageSync(k) {
    return wx.getStorageSync(k);
}

function getStorageInfoSync() {
    return wx.getStorageInfoSync();
}

function startAccelerometer() {
    return wx.startAccelerometer();
}

function stopAccelerometer() {
    return wx.stopAccelerometer();
}

function startCompass() {
    return wx.startCompass();
}

function stopCompass() {
    return wx.stopCompass();
}

function removeStorageSync(k) {
    try {
        wx.removeStorageSync(k);
    } catch (e) {
        throw e;
    }
}

function clearStorageSync() {
    wx.clearStorageSync();
}

function getSystemInfoSync() {
    try {
        return wx.getSystemInfoSync();
    } catch (e) {
        throw e;
    }
}

function hidePageLoading() {
    hideNavigationBarLoading();
    wx.hideLoading();
}

function pageScrollTo(d) {
    var duration = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 300;

    wx.pageScrollTo({
        scrollTop: d,
        duration: duration
    });
}

function showModal(title, content) {
    return new Promise(function (resolve, reject) {
        wx.showModal({
            title: title,
            content: content,
            success: function success(res) {
                if (res.confirm) {
                    resolve(res);
                } else if (res.cancel) {
                    reject();
                }
            },
            fail: function fail(e) {
                reject(e);
            }
        });
    });
}

function compareVersion(v1, v2) {
    v1 = v1.split('.');
    v2 = v2.split('.');
    var len = Math.max(v1.length, v2.length);

    while (v1.length < len) {
        v1.push('0');
    }
    while (v2.length < len) {
        v2.push('0');
    }

    for (var i = 0; i < len; i++) {
        var num1 = parseInt(v1[i]);
        var num2 = parseInt(v2[i]);

        if (num1 > num2) {
            return 1;
        } else if (num1 < num2) {
            return -1;
        }
    }

    return 0;
}